package com.zhongying.jf.push;

import com.zhongying.jf.NotificationSendInterface;
import com.zhongying.jf.push.ios.*;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

@Service("iOSNotificationSendImplement")
public class IOSNotificationSendImplement implements NotificationSendInterface {

	private String appkey = "5771dc0a67e58e8705003165";
	private String appMasterSecret = "x90oopijgupmnnkgfqvn3uucc23ffddw";
	private String after_open_default = "go_app";

    //广播--向安装该App的所有设备发送消息。
	public void sendBroadcast(String ticker, String title, String text, String after_open, String activity) throws Exception {
		IOSBroadcast broadcast = new IOSBroadcast();
		broadcast.setAppMasterSecret(appMasterSecret);
		broadcast.setPredefinedKeyValue("appkey", this.appkey);
		broadcast.setPredefinedKeyValue("timestamp", Integer.toString((int)(System.currentTimeMillis() / 1000)));

		broadcast.setPredefinedKeyValue("alert", text);
		broadcast.setPredefinedKeyValue("badge", 1);
		broadcast.setPredefinedKeyValue("sound", "chime");
        broadcast.setCustomizedField("activity",activity);
		// TODO set 'production_mode' to 'true' if your app is under production mode
		broadcast.setPredefinedKeyValue("production_mode", "true");
		// Set customized fields
		broadcast.setCustomizedField("test", "helloworld");
		broadcast.send();
	}

    //单播--向指定的设备发送消息
	public void sendUnicast(String ticker, String title, String text , String device_tokens , String after_open, String activity) throws Exception {
		IOSUnicast unicast = new IOSUnicast();
		unicast.setAppMasterSecret(appMasterSecret);
		unicast.setPredefinedKeyValue("appkey", this.appkey);
		unicast.setPredefinedKeyValue("timestamp", Integer.toString((int)(System.currentTimeMillis() / 1000)));
		// TODO Set your device token
		unicast.setPredefinedKeyValue("device_tokens", device_tokens);
		unicast.setPredefinedKeyValue("alert", text);
		unicast.setPredefinedKeyValue("badge", 0);
		unicast.setPredefinedKeyValue("sound", "chime");
        unicast.setCustomizedField("activity",activity);
		// TODO set 'production_mode' to 'true' if your app is under production mode
		unicast.setPredefinedKeyValue("production_mode", "true");

		unicast.send();
	}
	
	public void sendIOSGroupcast() throws Exception {
		IOSGroupcast groupcast = new IOSGroupcast();
		groupcast.setAppMasterSecret(appMasterSecret);
		groupcast.setPredefinedKeyValue("appkey", this.appkey);
		groupcast.setPredefinedKeyValue("timestamp", Integer.toString((int)(System.currentTimeMillis() / 1000)));
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONObject testTag = new JSONObject();
		testTag.put("tag", "iostest");
		tagArray.put(testTag);
		whereJson.put("and", tagArray);
		filterJson.put("where", whereJson);
		System.out.println(filterJson.toString());
		
		// Set filter condition into rootJson
		groupcast.setPredefinedKeyValue("filter", filterJson);
		groupcast.setPredefinedKeyValue("alert", "IOS 组播测试");
		groupcast.setPredefinedKeyValue("badge", 0);
		groupcast.setPredefinedKeyValue("sound", "chime");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		groupcast.setPredefinedKeyValue("production_mode", "true");
		groupcast.send();
	}
	
	public void sendIOSCustomizedcast() throws Exception {
		IOSCustomizedcast customizedcast = new IOSCustomizedcast();
		customizedcast.setAppMasterSecret(appMasterSecret);
		customizedcast.setPredefinedKeyValue("appkey", this.appkey);
		customizedcast.setPredefinedKeyValue("timestamp", Integer.toString((int)(System.currentTimeMillis() / 1000)));
		// TODO Set your alias here, and use comma to split them if there are multiple alias.
		// And if you have many alias, you can also upload a file containing these alias, then 
		// use file_id to send customized notification.
		customizedcast.setPredefinedKeyValue("alias", "xx");
		// TODO Set your alias_type here
		customizedcast.setPredefinedKeyValue("alias_type", "xx");
		customizedcast.setPredefinedKeyValue("alert", "IOS 个性化测试");
		customizedcast.setPredefinedKeyValue("badge", 0);
		customizedcast.setPredefinedKeyValue("sound", "chime");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		customizedcast.setPredefinedKeyValue("production_mode", "true");
		customizedcast.send();
	}
	

	public void sendFilecast(String ticker, String title, String text , List<String> device_tokens, String after_open, String activity) throws Exception {
        HashSet<String> hashSet = new HashSet<>(device_tokens);
        String contents = "";
        for(Iterator<String> token = hashSet.iterator(); token.hasNext();) {
            contents += token.next() +"\n";
        }
		IOSFilecast filecast = new IOSFilecast();
		filecast.setAppMasterSecret(appMasterSecret);
		filecast.setPredefinedKeyValue("appkey", this.appkey);
		filecast.setPredefinedKeyValue("timestamp", Integer.toString((int)(System.currentTimeMillis() / 1000)));
		// TODO upload your device tokens, and use '\n' to split them if there are multiple tokens 
		filecast.uploadContents(contents);
		filecast.setPredefinedKeyValue("alert", text);
		filecast.setPredefinedKeyValue("badge", 1);
		filecast.setPredefinedKeyValue("sound", "chime");
        filecast.setCustomizedField("activity",activity);
		// TODO set 'production_mode' to 'true' if your app is under production mode
		filecast.setPredefinedKeyValue("production_mode", "true");
		filecast.send();
	}

}
